import React, { useReducer, useMemo, createContext } from 'react';
import context from './context';
import reducer from './reducer';
import initial from './initial';

export default ({children}: any) => {
  //利用useReducer，传入2参数，第一个reducer处理数据的地方，第二个为存储数据的地方
  //解构出dispatch：用来派遣action，在reducer中根据action改变数据
  //解构出state： 为存储的数据，本例子为：state：{ numberValue: 0}
  const [state, dispatch] = useReducer(reducer, initial);
  const contextValue = useMemo(() => ({
    state,
    dispatch,
  }), [
    state
  ]);
return (
    <context.Provider
      value={contextValue}
    >
      {children}
    </context.Provider>
  );
}
